package com.zbb.comm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zbb.comm.feignclient.UserServiceClient;
import com.zbb.comm.mapper.CroupChatContactsMapper;
import com.zbb.comm.po.CroupChatContact;
import com.zbb.comm.po.CroupChatContactVo;
import com.zbb.comm.po.User;
import com.zbb.comm.service.CroupChatContactsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CroupChatContactsServiceImpl implements CroupChatContactsService {
    @Autowired
    CroupChatContactsMapper croupChatContactsMapper;

    @Autowired
    UserServiceClient userServiceClient;


    @Override
    public List<CroupChatContactVo> selectCroupById(String userId) {
        QueryWrapper<CroupChatContact> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<CroupChatContact> croupChatContacts = croupChatContactsMapper.selectList(queryWrapper);
        List<CroupChatContactVo> croupChatContactVos = new ArrayList<>();
        for (CroupChatContact croupChatContact:croupChatContacts){
            CroupChatContactVo croupChatContactVo = new CroupChatContactVo();
            BeanUtils.copyProperties(croupChatContact,croupChatContactVo);
            queryWrapper.clear();
            queryWrapper.eq("group_room_id",croupChatContact.getGroupRoomId());
            List<CroupChatContact> croupChatContacts1 = croupChatContactsMapper.selectList(queryWrapper);
            List<User> users = new ArrayList<>();
            for (CroupChatContact croupChatContact1:croupChatContacts1){
                User user = userServiceClient.querytUser(croupChatContact1.getUserId());
                users.add(user);
            }
            croupChatContactVo.setCroupUserLists(users);
            croupChatContactVos.add(croupChatContactVo);
        }
        return croupChatContactVos;
    }

    @Override
    public List<String> selectCroupByRoomId(String roomId) {
        QueryWrapper<CroupChatContact> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_room_id",roomId);
        List<CroupChatContact> croupChatContacts = croupChatContactsMapper.selectList(queryWrapper);

        List<String> userIds = croupChatContacts.stream()
                .map(CroupChatContact::getUserId)
                .collect(Collectors.toList());
        return userIds;
    }
}
